package com.thinksns.sociax.t4.android.popupwindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.thinksns.sociax.android.R;
import com.thinksns.sociax.constant.AppConstant;
import com.thinksns.sociax.t4.android.Listener.ListenerSociax;
import com.thinksns.sociax.t4.android.Thinksns;
import com.thinksns.sociax.t4.android.fragment.FragmentSociax;
import com.thinksns.sociax.t4.android.function.FunctionThirdPlatForm;
import com.thinksns.sociax.t4.model.ModelEventDetail;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

import static com.thinksns.sociax.android.R.id.tv_more_delete;

/**
 * Created by zhiyicx on 2017/5/15.
 */

public class PopupWindowEventMore implements View.OnClickListener {
    private static final String TAG = "PopupWindowEventMore";
    private PopupWindow mPopupWindow;
    private LayoutInflater inflater;

    private TextView mTvComment, mTvTranspond, mTvCollection, mTvDenounce,
            mTvDelete, tv_share_to_sina, tv_share_to_weichat,
            tv_share_to_weichatfriends, tv_share_to_qq, tv_share_to_qqweibo,
            tv_share_to_qzone;

    private Context context;
    private Button btnCancel;
    private ModelEventDetail detail;
    private FragmentSociax fragment;
    private PopupWindowHandler handler;
    ListenerSociax listener;
    private Thinksns app;
    private PopupWindow popupWindowShowNotify;
    private View.OnClickListener delListener;

    public ListenerSociax getListener() {
        return listener;
    }

    public void setListener(ListenerSociax listener) {
        this.listener = listener;
    }

    /**
     * 帖子更多
     */
    public PopupWindowEventMore(Context context, ModelEventDetail detail) {
        super();
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        handler = new PopupWindowHandler();
        this.detail = detail;
        initPopuptWindow();
        app = Thinksns.getApplication();
    }

    /**
     * 帖子更多
     */
    public PopupWindowEventMore(Context context) {
        super();
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        handler = new PopupWindowHandler();
        initPopuptWindow();
        app = Thinksns.getApplication();
    }

    public void setEventDetial(ModelEventDetail detail) {
        if (detail != null)
            showFavStatus(detail.getStar());
        this.detail = detail;
        if (detail.getUid().equals(Thinksns.getMy().getUid() + "")) {
            mTvDelete.setVisibility(View.VISIBLE);
        }
    }

    public void setDeleteListener(View.OnClickListener delListener) {
        this.delListener = delListener;
    }

    /**
     * 获取PopupWindow实例
     */
    public PopupWindow getPopupWindowInstance() {
        if (null == mPopupWindow) {
            initPopuptWindow();
        }
        return mPopupWindow;
    }

    public void setStarClickListener(View.OnClickListener onClickListener) {
        mTvCollection.setOnClickListener(onClickListener);
    }

    /**
     * 创建PopupWindow
     */
    private void initPopuptWindow() {
        View popupWindow = inflater.inflate(R.layout.more_popupwindow, null);
        btnCancel = (Button) popupWindow.findViewById(R.id.btn_pop_cancel);
        btnCancel.setOnClickListener(this);
//		mTvComment = (TextView) popupWindow.findViewById(R.id.tv_more_comment);
        //转发
        mTvTranspond = (TextView) popupWindow
                .findViewById(R.id.tv_more_transpond);
        mTvTranspond.setVisibility(View.GONE);
        //收藏
        mTvCollection = (TextView) popupWindow
                .findViewById(R.id.tv_more_collection);
        mTvCollection.setText("关注");
        //举报
        mTvDenounce = (TextView) popupWindow
                .findViewById(R.id.tv_more_denounce);
        mTvDenounce.setVisibility(View.GONE);
        //删除
        mTvDelete = (TextView) popupWindow.findViewById(tv_more_delete);
        mTvDelete.setVisibility(View.GONE);

        tv_share_to_sina = (TextView) popupWindow
                .findViewById(R.id.tv_share_to_sinaweibo);
        tv_share_to_weichat = (TextView) popupWindow
                .findViewById(R.id.tv_share_to_weichat);
        tv_share_to_weichatfriends = (TextView) popupWindow
                .findViewById(R.id.tv_share_to_weichatfav);
        tv_share_to_qq = (TextView) popupWindow
                .findViewById(R.id.tv_share_to_qq);
        tv_share_to_qqweibo = (TextView) popupWindow
                .findViewById(R.id.tv_share_to_qqweibo);
        tv_share_to_qzone = (TextView) popupWindow
                .findViewById(R.id.tv_share_to_qzone);

//		mTvComment.setOnClickListener(this);
        mTvTranspond.setOnClickListener(this);
        tv_share_to_weichat.setOnClickListener(this);
        tv_share_to_weichatfriends.setOnClickListener(this);
        tv_share_to_sina.setOnClickListener(this);
        tv_share_to_qq.setOnClickListener(this);
        tv_share_to_qzone.setOnClickListener(this);
        tv_share_to_qqweibo.setOnClickListener(this);
        mTvDelete.setOnClickListener(this);
        mTvDenounce.setVisibility(View.GONE);
//		mTvDelete.setVisibility(View.GONE);


        // 创建一个PopupWindow
        // 参数1：contentView 指定PopupWindow的内容
        // 参数2：width 指定PopupWindow的width
        // 参数3：height 指定PopupWindow的height
        mPopupWindow = new PopupWindow(popupWindow, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setFocusable(true);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setAnimationStyle(R.style.popUpwindow_anim);
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                dismiss();
            }
        });
    }

    public void dismiss() {
        WindowManager.LayoutParams layoutParams = ((Activity) context).getWindow().getAttributes();
        layoutParams.alpha = 1.0f;
        ((Activity) context).getWindow().setAttributes(layoutParams);
        mPopupWindow.dismiss();
    }

    public void show(View v) {
        WindowManager.LayoutParams layoutParams = ((Activity) v.getContext()).getWindow().getAttributes();
        layoutParams.alpha = 0.5f;
        ((Activity) v.getContext()).getWindow().setAttributes(layoutParams);
        mPopupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
    }

    @Override
    public void onClick(final View view) {
        switch (view.getId()) {
            case R.id.btn_pop_cancel:
                mPopupWindow.dismiss();
                break;

            case R.id.tv_share_to_sinaweibo:
                onSinaWeiboShare();
                break;
            case R.id.tv_share_to_weichat:
                onweichatShare();
                break;
            case R.id.tv_share_to_qq:
                onQQShare();
                break;
            case R.id.tv_share_to_weichatfav:
                onWeichatMovementShare();
                break;
            case R.id.tv_share_to_qzone:
                onQzoneShare();
                break;
            case R.id.tv_more_delete:
                showDeletePop();
                break;
            default:
                break;
        }
    }

    public void showDeletePop() {
        popupWindowShowNotify = new PopupWindowShowNotify
                .Builder(context)
                .setTitle("温馨提示")
                .setContent("确认要删除吗?")
                .setOk("确定", delListener)
                .setCancel("取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popupWindowShowNotify.dismiss();
                    }
                })
                .build();
        popupWindowShowNotify.showAtLocation(((Activity) context).getWindow().getDecorView(), Gravity.CENTER, 0, 0);
    }

    /**
     * 分享到QQ空间
     */
    private void onQzoneShare() {
        FunctionThirdPlatForm fc_share = new FunctionThirdPlatForm(context,
                ShareSDK.getPlatform(QZone.NAME));
        fc_share.doShareEvent(detail);
    }

    /**
     * 分享到朋友圈
     */
    private void onWeichatMovementShare() {
        FunctionThirdPlatForm fc_share = new FunctionThirdPlatForm(context,
                ShareSDK.getPlatform(WechatMoments.NAME));
        fc_share.doShareEvent(detail);
    }

    /**
     * 分享到微信
     */
    private void onweichatShare() {
        FunctionThirdPlatForm fc_share = new FunctionThirdPlatForm(context,
                ShareSDK.getPlatform(Wechat.NAME));
        fc_share.doShareEvent(detail);
    }

    /**
     * 分享到QQ
     */
    private void onQQShare() {
        FunctionThirdPlatForm fc_share = new FunctionThirdPlatForm(context,
                ShareSDK.getPlatform(QQ.NAME));
        fc_share.doShareEvent(detail);
    }

    /**
     * 分享到微博
     */
    private void onSinaWeiboShare() {
        FunctionThirdPlatForm fc_share = new FunctionThirdPlatForm(context,
                ShareSDK.getPlatform(SinaWeibo.NAME));
        fc_share.doShareEvent(detail);
    }

    class PopupWindowHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case AppConstant.FAVORITE:
                    // if (msg.arg1 == 1) {
                    // ModelBackMessage message = (ModelBackMessage) msg.obj;
                    // if (message.getStatus() == 1) {
                    // Toast.makeText(context, "收藏成功", Toast.LENGTH_SHORT)
                    // .show();
                    // post.setFavorited(true);
                    // mPopupWindow.dismiss();
                    // } else {
                    // Toast.makeText(context, "收藏失败", Toast.LENGTH_SHORT)
                    // .show();
                    // }
                    // }
                    break;
                case AppConstant.UNFAVORITE:
                    // if (msg.arg1 == 1) {
                    // ModelBackMessage message = (ModelBackMessage) msg.obj;
                    // if (message.getStatus() == 1) {
                    // Toast.makeText(context, "取消收藏成功", Toast.LENGTH_SHORT)
                    // .show();
                    // post.setFavorited(false);
                    // mPopupWindow.dismiss();
                    // } else {
                    // Toast.makeText(context, "取消收藏失败", Toast.LENGTH_SHORT)
                    // .show();
                    // }
                    // }
                    break;
                default:

                    break;
            }
        }
    }

    /**
     * 显示关注状态
     *
     * @param isFav
     */
    public void showFavStatus(boolean isFav) {
        if (isFav) {
            mTvCollection.setCompoundDrawablesWithIntrinsicBounds(0,
                    R.drawable.ic_more_collected, 0, 0);
            mTvCollection.setText("取消关注");
        } else {
            mTvCollection.setCompoundDrawablesWithIntrinsicBounds(0,
                    R.drawable.ic_more_collect, 0, 0);
            mTvCollection.setText("关注");
        }
    }

    /**
     * 自定义分享个数
     *
     * @return
     */
    public List<Map<String, Object>> getSharedByData() {
        List<Map<String, Object>> sharedList = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < 6; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("bgId", R.drawable.comment);
            map.put("text", "微信" + i);
            sharedList.add(map);
        }
        return sharedList;
    }
}
